package com.xjt.wx_admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjt.wx_admin.entity.MsgRecord;
import com.xjt.wx_admin.entity.UserLatelyMsg;
import com.xjt.wx_admin.entity.dto.SendMsg;
import com.xjt.wx_admin.service.MsgRecordService;
import com.xjt.wx_admin.service.UserLatelyMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/msg")
public class MsgController {


    @Autowired
    MsgRecordService msgRecordService;

    @Autowired
    UserLatelyMsgService userLatelyMsgService;

    @GetMapping("/getMsgRecord")
    public R getMsgRecord(Page page, @NotNull Long toId){
        IPage<MsgRecord> list = msgRecordService.queryUserMsg(page,toId);
        return R.ok(list);
    }


    @GetMapping("/getLatelyMsg")
    public R getLatelyMsg(){
        List<UserLatelyMsg> latelyMsgs = userLatelyMsgService.queryLatelyMsg();
        return R.ok(latelyMsgs);
    }

    /**
     * 发送消息
     *
     * @param sendMsg 发送消息
     * @return {@link R}
     */
    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody @Valid SendMsg sendMsg){
        return R.ok(msgRecordService.sendMsg(sendMsg));
    }
}
